package com.xiaoyu.client.ui.activity.forum;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.forum.ForumReplyOneRecyAdapter;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.help.HelpDetailActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 悬浮编辑框
 */
public class ForumFloatEditorActivity extends Activity implements View.OnClickListener {
    @BindView(R.id.dialog_forum_float_editor_cancle)
    TextView cancelBtn;
    @BindView(R.id.dialog_forum_float_editor_send)
    TextView sendBtn;
    @BindView(R.id.dialog_forum_float_editor_edit_box)
    EditText replyEdit;

    private Context mContext;
    private String id;//求助或论坛帖子id
    private String replyId;//一级回复id
    private int from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forum_float_editor);
        mContext = this;
        ButterKnife.bind(this);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
        this.setFinishOnTouchOutside(true);
        parseIntent();
        initData();
    }

    private void parseIntent() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            from = bundle.getInt(ClientConstants.FROM);
            if (from == ClientConstants.FROM_SEEK_DETAIL) {//求助详情
                id = bundle.getString(HelpDetailActivity.SEEK_ID);
                replyId = bundle.getString(HelpDetailActivity.REPLY_ID);
            }
            if (from == ClientConstants.FROM_FORUM_DETAIL) {//论坛帖子详情
                id = bundle.getString(ForumReplyOneRecyAdapter.FORUM_ID);
                replyId = bundle.getString(ForumReplyOneRecyAdapter.REPLY_ID);
            }
        }
    }

    private void initData() {
        cancelBtn.setOnClickListener(this);
        sendBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == cancelBtn) {
            finish();
        } else if (v == sendBtn) {
            if (TextUtils.isEmpty(replyEdit.getText().toString())) {
                ToastUtil.showToast(this, "请输入评论");
                return;
            }
            if (from == ClientConstants.FROM_SEEK_DETAIL) {//求助详情
                sendReplySeek();
            }else if (from == ClientConstants.FROM_FORUM_DETAIL){//论坛帖子详情
                sendReplyForum();
            }
        }
    }


    /**
     * 论坛帖子回复的回复
     */
    private void sendReplyForum() {
        NetworkManager.sendForumReply(id, replyEdit.getText().toString(), replyId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ForumFloatEditorActivity.this.finish();

            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    /**
     * 求助回复
     */
    private void sendReplySeek() {
        NetworkManager.sendSeekReply(id, replyId, replyEdit.getText().toString(), new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                ForumFloatEditorActivity.this.finish();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }
}
